<template>
  <div class="achievement-badge" :class="{ 'earned': earned }">
    <div class="badge-icon" :style="{ backgroundImage: `url(${iconUrl})` }">
      <div v-if="!earned" class="badge-lock">
        <el-icon><Lock /></el-icon>
      </div>
    </div>
    <div class="badge-info">
      <div class="badge-name">{{ name }}</div>
      <div class="badge-description">{{ description }}</div>
    </div>
    <div v-if="earned" class="badge-earned-date">{{ formattedEarnedDate }}</div>
    
    <!-- 领取积分按钮直接显示在卡片上 -->
    <div v-if="earned && !pointsClaimed" class="claim-points-inline">
      <el-button type="primary" @click.stop="claimPoints" size="small">
        领取积分
      </el-button>
    </div>
    <div v-else-if="earned && pointsClaimed" class="points-claimed-inline">
      <el-tag type="success" size="small">已领取</el-tag>
    </div>
  </div>

</template>

<script setup>
import { ref, computed } from 'vue';
import { Lock } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { claimAchievementPoints } from '@/api/achievement';

const props = defineProps({
  // 用户成就ID（用于领取积分）
  id: {
    type: Number,
    required: true
  },
  // 成就名称
  name: {
    type: String,
    required: true
  },
  // 成就描述
  description: {
    type: String,
    required: true
  },
  // 成就图标URL
  iconUrl: {
    type: String,
    required: true
  },
  // 是否已获得
  earned: {
    type: Boolean,
    default: false
  },
  // 获得时间
  earnedDate: {
    type: String,
    default: ''
  },
  // 条件类型
  conditionType: {
    type: String,
    required: true
  },
  // 条件值
  conditionValue: {
    type: Number,
    required: true
  },
  // 奖励积分
  pointsReward: {
    type: Number,
    default: 0
  },
  // 积分是否已领取
  pointsClaimed: {
    type: Boolean,
    default: false
  }
});

// 定义emit事件
const emit = defineEmits(['points-claimed']);

// 格式化获得时间
const formattedEarnedDate = computed(() => {
  if (!props.earnedDate) return '';
  
  try {
    const date = new Date(props.earnedDate);
    return date.toLocaleString('zh-CN', {
      year: 'numeric',
      month: '2-digit',
      day: '2-digit',
      hour: '2-digit',
      minute: '2-digit'
    });
  } catch (e) {
    return props.earnedDate;
  }
});

// 领取积分
const claimPoints = async () => {
  try {
    // @ts-ignore
    const res = await claimAchievementPoints(props.id);
    // @ts-ignore
    if (res.code === 200) {
      ElMessage.success('积分领取成功！');
      // 通知父组件更新用户积分
      emit('points-claimed', res.data.points);
    } else {
      // @ts-ignore
      ElMessage.error(res.msg || '领取失败');
    }
  } catch (error) {
    console.error('领取积分失败:', error);
    ElMessage.error('领取积分失败，请稍后重试');
  }
};
</script>

<style scoped>
.achievement-badge {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 140px;
  height: 210px;
  padding: 16px;
  border-radius: 8px;
  background-color: #f5f7fa;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  cursor: default;
  position: relative;
  overflow: hidden;
  border: 1px solid #ebeef5;
}

.achievement-badge:hover {
  transform: translateY(-4px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.achievement-badge.earned {
  background-color: #e6f7ff;
  border: 1px solid #bae7ff;
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.1);
}

/* 移除了右上角的橙色标志 */

.badge-icon {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  margin-bottom: 16px;
  display: flex;
  justify-content: center;
  align-items: center;
  background-color: #f0f0f0;
  position: relative;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  transition: all 0.3s ease;
  border: 2px solid white;
  z-index: 2;
}

.achievement-badge.earned .badge-icon {
  box-shadow: 0 2px 8px rgba(24, 144, 255, 0.2);
  border: 2px solid #e6f7ff;
}

.achievement-badge:hover .badge-icon {
  transform: scale(1.05);
}

.badge-lock {
  position: absolute;
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.6);
  display: flex;
  justify-content: center;
  align-items: center;
  color: white;
  font-size: 24px;
}

.badge-info {
  text-align: center;
  flex-grow: 1;
  display: flex;
  flex-direction: column;
  justify-content: center;
}

.badge-name {
  font-weight: 600;
  font-size: 15px;
  margin-bottom: 8px;
  color: #303133;
  text-align: center;
  width: 100%;
}

.badge-description {
  font-size: 12px;
  color: #606266;
  line-height: 1.4;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
  text-overflow: ellipsis;
  text-align: center;
  width: 100%;
}

.badge-earned-date {
  font-size: 12px;
  color: #52c41a;
  margin-top: 10px;
  padding: 4px 10px;
  background-color: rgba(82, 196, 26, 0.1);
  border-radius: 12px;
  font-weight: 500;
  position: relative;
  z-index: 2;
}

/* 内联领取积分按钮样式 */
.claim-points-inline {
  margin-top: 8px;
  width: 100%;
  text-align: center;
  z-index: 2;
}

.claim-points-inline .el-button {
  width: 100%;
  font-size: 12px;
  padding: 6px 12px;
}

.points-claimed-inline {
  margin-top: 8px;
  width: 100%;
  text-align: center;
  z-index: 2;
}

/* 响应式调整 */
@media (max-width: 768px) {
  .achievement-badge {
    width: 100px;
    height: 130px;
    padding: 8px;
    /* 苹果风格：保持圆角 */
    border-radius: 12px;
  }
  
  .badge-icon {
    width: 50px;
    height: 50px;
    /* 苹果风格：保持圆形设计 */
    border-radius: 50%;
  }
  
  .badge-name {
    font-size: 12px;
    /* 苹果风格：保持字体平滑 */
    -webkit-font-smoothing: antialiased;
  }
  
  .badge-description {
    font-size: 10px;
    /* 苹果风格：保持字体平滑 */
    -webkit-font-smoothing: antialiased;
  }
  
  .badge-earned-date {
    /* 苹果风格：保持圆角 */
    border-radius: 10px;
    font-size: 10px;
  }
  
  .details-icon {
    width: 60px;
    height: 60px;
    /* 苹果风格：保持圆形设计 */
    border-radius: 50%;
  }
  
  .details-content h3 {
    font-size: 16px;
    /* 苹果风格：保持字体权重 */
    font-weight: 600;
  }
}
</style>